12. Exercise: Style your Notification
L1 A10 Style Your Notification
Android Developer Documentation
Exercise
- Open
NotificationUtils.ktand find thesendNotificationfunction.
- Start with loading the image from resources by using the
BitmapFactory.
// TODO: Step 2.0 add style
val eggImage = BitmapFactory.decodeResource(
applicationContext.resources,
R.drawable.cooked_egg
)
- Once you have the image, create a new
BigPictureStyleand set your image.
- You also need to set the
bigLargeIcon()tonullso the large icon goes away when the notification is expanded.
// NotificationUtils.kt
// TODO: Step 2.0 add style
val eggImage = BitmapFactory.decodeResource(
applicationContext.resources,
R.drawable.cooked_egg
)
val bigPicStyle = NotificationCompat.BigPictureStyle()
.bigPicture(eggImage)
.bigLargeIcon(null)
- Set the new style to
notificationBuilderby callingsetStyle()on the builder object.
- Set your image as
setLargeIconon the builder so the image will be displayed as a smaller icon when notification is collapsed.
// NotificationUtils.kt
// TODO: Step 2.1 add style to builder
.setStyle(bigPicStyle)
.setLargeIcon(eggImage)
- Run the app and set a timer. When the notification is first shown, it is in the collapsed state in the notification drawer. If you expand the notification, a large image is shown in the extended notification area.